<%-- 
    Document   : list_response
    Created on : Apr 25, 2012, 8:16:21 PM
    Author     : acer
--%>

<%@page import="model.ListContentModel"%>
<%@page trimDirectiveWhitespaces="true"%>
<%@include file="connect.jspf"%>
<%
    String[] op = request.getParameter("operation").split("\\@");
    ListContentModel listmodel = new ListContentModel(con);
    String uid = (session.getAttribute("userid")!=null) ? session.getAttribute("userid").toString():"";
    String tag = (request.getParameter("tag")!=null)? request.getParameter("tag"):"";
    String filtermode = (request.getParameter("filtermode")!=null)? request.getParameter("filtermode"):"";
    String sortmode = (request.getParameter("sortmode")!=null)? request.getParameter("sortmode"):"";
    int last = (request.getParameter("last")!=null)? Integer.parseInt(request.getParameter("last")):0;//untuk navigasi page
    int maxyscroll=(request.getParameter("maxyscroll")!=null)? Integer.parseInt(request.getParameter("maxyscroll")):0;//maximum post yang ditampilkan di daftar kontent
    boolean permittoquery=(request.getParameter("permission")!=null)? Boolean.getBoolean(request.getParameter("permission")):false;//maximum post yang ditampilkan di daftar kontent
    int maxinpage = 10;

    if(op[0].equals("like") || op[0].equals("dislike"))
	{
            
               if(!uid.isEmpty()) //jika sudah login maka boleh melakukan like
		{
			if(!listmodel.isPostLikedByUser(Integer.parseInt(uid),Integer.parseInt(op[1])))
			{
				listmodel.doOperation(request.getParameter("operation"));
				out.write(listmodel.fetchLikeQuery(Integer.parseInt(op[1]),Integer.parseInt(uid),op[0]));
			}
			else if (listmodel.isUserOperationSame(Integer.parseInt(uid),Integer.parseInt(op[1]),op[0]))//netralkan nilai
			{
				out.write(listmodel.neutralizeLikeCount(Integer.parseInt(uid),Integer.parseInt(op[1]),op[0]));
			}
			else if (!listmodel.isUserOperationSame(Integer.parseInt(uid),Integer.parseInt(op[1]),op[0]))//lakukan penetralan dan operasi kebalikan
			{
				String invertop = (listmodel.getLikeOrDislike(Integer.parseInt(uid),Integer.parseInt(op[1])).equals("like"))? "dislike":"like";
				out.write(listmodel.neutralizeLikeCount(Integer.parseInt(uid),Integer.parseInt(op[1]),listmodel.getLikeOrDislike(Integer.parseInt(uid),Integer.parseInt(op[1]))));
				listmodel.doOperation(invertop+"@"+op[1]);
				listmodel.fetchLikeQuery(Integer.parseInt(op[1]),Integer.parseInt(uid),invertop);
			}
		}
		else out.write("error Please Login or Register First ");
	}
	else if (op[0].equals("scroll") && Integer.parseInt(op[1])>= maxyscroll && permittoquery==false)
	{
		out.write("plus ");
		last+=maxinpage;
		permittoquery=true;
	}
	if (op[0].equals("scroll") && Integer.parseInt(op[1])>=maxyscroll && permittoquery == true)
	{
		String query = listmodel.generateListQuery(tag,filtermode,sortmode,last,maxinpage);
		String count_query = listmodel.generateCountListQuery(tag,filtermode,sortmode,last,maxinpage);
		int datarow = listmodel.getResultFromQuery(count_query).getRow();
		if(last < datarow)
		{
			listmodel.listPost(query,Integer.parseInt(uid));
		}
	}
	else if(op[0]=="scroll" && Integer.parseInt(op[1])<=maxyscroll) out.write("error");
    
%>
